package com.jiezuo.flowable.controller;

import com.jiezuo.common.base.BaseController;
import com.jiezuo.common.util.ResultUtil;
import com.jiezuo.common.vo.Result;
import com.jiezuo.flowable.service.UserResourceService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.repository.Deployment;
import org.flowable.ui.modeler.model.ModelRepresentation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description 资源管理
 * @author 庞留杰
 * @date 2020/9/11 18:01
 * @version V1.0
 **/
@Slf4j
@Transactional
@RestController
@RequestMapping("/resource")
public class UserResourceController extends BaseController {

    @Autowired
    private UserResourceService service;

    /**
     * 功能描述: 查看流程图片/XML【查看图片：只是一个图片，不会显示执行到那个节点（流程执行完毕 ！！！）】
     *          访问示例： http://localhost:8888/model/resource?procDefId=askForLeave:1:8b0c7d46-f30c-11ea-a46e-405bd8b55521&proInsId=c6dc39f7-f30c-11ea-a46e-405bd8b55521&resType=image
     * @author: 庞留杰
     * @param processDefinitionId:流程定义ID
     * @param processInstanceId:流程实例ID
     * @param resourceType:资源类型(xml|image)
     * @return:
     * @exception
     * @date: 2020/9/11 19:04
     */
    @GetMapping("/resourceRead")
    public void resourceRead(String processDefinitionId, String processInstanceId, String resourceType, HttpServletResponse response) throws Exception {
        service.resourceRead(processDefinitionId, processInstanceId, resourceType, response);
    }

    /**
     * 功能描述: 生成当前流程图表【会显示执行到那个节点，但是一旦流程结束，图片便会失效，只可以用作执行过程中的流程图展示（流程正在执行中...）】
     * @author: 庞留杰
     * @param processInstanceId:流程实例Id
     * @return:
     * @exception
     * @date: 2020/9/11 19:21
     */
    @GetMapping(value = "processDiagram")
    public void getProcessDiagram(String processInstanceId, HttpServletResponse response) throws Exception {
        service.getProcessDiagram(processInstanceId, response);
    }

    
    /**
    * 功能描述: 获取流程图（包含 正在进行中+已结束 的流程图）
    * @author: 庞留杰
    * @param processInstanceId 流程实例ID
    * @param executionId 执行实例ID（只用并行、多实例中才存在）
    * @return:
    * @exception   
    * @date: 2021/4/16 13:43
    */
    @GetMapping(value = "flowDiagram")
    public void flowDiagram(String processInstanceId, @RequestParam(required = false) String executionId, HttpServletResponse response) throws Exception {
        service.flowDiagram(processInstanceId, executionId, response);
    }

    /**
     * 功能描述: 下载xml
     *       摘抄源码：org.flowable.ui.modeler.rest.app -> /rest/models/{processModelId}/bpmn20
     * @author: 庞留杰
     * @param processDefinitionId:流程定义ID
     * @return:
     * @exception
     * @date: 2020/9/11 17:57
     */
    @GetMapping("/resource/downloadXml/{deploymentId}")
    public void downloadXml(@PathVariable("deploymentId") String processDefinitionId, HttpServletResponse response) {
        service.downloadXml(processDefinitionId, response);
    }


    @PostMapping(value = "/files/zip", headers = "content-type=multipart/form-data")
    //部署压缩包形式的模板(.zip .bar)，主子流程定义部署", notes = "可用于一次性部署多个资源文件（.bpmn .drl .form等）")
    public Result deployByZip(String name, String category, String tenantId, MultipartFile file) {
        Deployment deployment = service.deployByZip(name, category, tenantId, file);
        return new ResultUtil<>().setData(deployment);
    }

    /**
     * 功能描述: 利用此方式,可以将文件导入到"act_de_model"中
     *           摘抄源码: org.flowable.ui.modeler.rest -> POST "app/rest/import-case-model"
     * @author: 庞留杰
     * @param file
     * @return: Result
     * @exception
     * @date: 2020/9/21 10:48
     */
    //导入流程模板文件", notes = "导入流程模板文件")
    //(name = "file", value = "模板文件", required = true, dataType = "__file")
    @PostMapping(value = "/importModel", produces = "application/json")
    public Result importProcessModel(@RequestParam("file") MultipartFile file,HttpServletRequest request ) {
        ModelRepresentation modelRepresentation = service.importProcessModel(file, request);
        return new ResultUtil<>().setData(modelRepresentation);
    }

}
